﻿$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/pythonarticleinfo/queryList',
        datatype: "json",
        colModel: [
			{ label: '状态', name: 'id', index: 'id',align: "center", width: 40, key: true, formatter:function(value,options,rowData){
					var isDel=rowData["isDel"];
					var exption=rowData["exption"];
					var isImport=rowData["isImport"];
					if(isImport==-1){
						return "<p style='color:blue'>"+'编辑中'+"</p>"
					}else if(isImport==0){
						return "<p style='color:red'>"+'未处理'+"</p>"
					}else if(isImport==1){
						return "<p style='color:green'>"+'已入库'+"</p>"
					}
				}
			},
			// { label: '是否删除', name: 'isDel', index: 'isDel',align: "center", width: 50, key: true,bool:"hidden", formatter:function(value,options,rowData){
			// 		if(1==1) {
			// 			$("#jqGrid").setGridParam().hideCol("isDel");
			// 			return "<a style='display: none'>" + value + "</a>"
			// 		}
			// 	}
			// },
			// { label: '状态', name: 'exption', index: 'exption',align: "center", width: 80, formatter:function(value,options,rowData){
			// 	var isDel=rowData["isDel"];
			// 	if(isDel==0){
			// 		return "<p style='color: red'>"+'已删除'+"</p>"
			// 	}else if(value=="正常"){
			// 		return "<p style='color: #009688'>"+'待入库'+"</p>"
			// 	}else{
			// 		return "<p style='color: #d73925'>"+'待删除'+"</p>"
			// 	}
			// 	}
			// },
			{ label: '爬取时间', name: 'time', index: 'time',align: "center", width: 60 },
			{ label: '公众号', name: 'weixinNickname', index: 'weixinNickname',align: "center", width: 80 },
			{ label: '文章标题', name: 'articleTitle', index: 'articleTitle',align: "center", width: 180 },
			{ label: '品牌/分类', name: 'brandName', index: 'brandName',align: "center", width: 40 },
			{ label: '编辑人', name: 'updateUser', index: 'updateUser',align: "center", width: 40 },
			// { label: '来源', name: 'weixinNickname', index: 'weixinNickname',align: "center", width: 80 },
			// { label: '未入库原因', name: 'exption', index: 'exption',align: "center", width: 80 },
			// { label: '浏览', name: 'weixinTmpUrl', index: 'weixin_tmp_url',align: "center", width: 80, formatter:function(value,options,rowData){
			// 		return "<a href='"+value+"'>"+'原文'+"</a>";
			// 	}
			// },
			{ label: '操作', name: 'cz', index: 'cz',align: "center", width: 50,formatter:function(value,options,rowData) {
                // "<button>"+'入库'+"</button>"+
                var id = rowData["id"];
                var isDel = rowData["isDel"];
                var exption = rowData["exption"];
                var url = rowData["weixinTmpUrl"];

                // var buttons = "<button class='btn btn-primary' onclick='viewHtml(" + id + ")'>" + '浏览' + "</button>";
                var buttons ="";
                // if (exption == "未入库") {
                //     buttons = buttons + "<button class='btn btn-primary' onclick='importHtml(" + id + ")'>" + '导入数据' + "</button>";
                // }

                //if (isDel == 0) {
                    //buttons = buttons + "<button class='btn btn-primary' onclick='del(" + id + ",1)'>" + '恢复' + "</button>";
                //} else {
                    // "<button class='btn btn-primary'>"+'入库'+"</button>"+
                    //buttons = buttons + "<button class='btn btn-primary' onclick='del(" + id + ",0)'>" + '删除' + "</button>";
                //}
					buttons = buttons + "<a href=\""+url+"\" target=\"_blank\">原文</a>";
					buttons = buttons + "&nbsp;&nbsp;<a href=\"../sys/python_edit.html?id="+id+"\" target=\"_blank\">编辑</a>"
					buttons = buttons + "&nbsp;&nbsp;<a href='javascript:void(0)' onclick='del(" + id + ")'>删除</a>";
                return buttons;
            }
			},
        ],
		viewrecords: true,
        height: 520,
        rowNum: 15,
		rowList : [15,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
		postData:{"articleTitle":vm.q.articleTitle,"brandName":vm.q.brandName,"weixinNickname":vm.q.weixinNickname},
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
			$.ajax({
				type: "POST",
				async:false,
				url: baseURL + 'sys/pythonarticleinfo/dataList',
				contentType: "application/json",
				success: function(r){
					if(r.code==0){
						$("#brandName").html('');
						for(i=0;i<r.dataList.length;i++){
							$("#brandName").append(
								"<option value=" + r.dataList[i].brandName + "/>");
						}
					}
				}
			});

        }
    });

});
function del(e,x) {
	var id=e;
	vm.pythonarticleinfo.id=e;
	confirm('确定删除该文章？', function() {
		$.ajax({
			type: "POST",
			url: baseURL + "sys/pythonarticleinfo/delById",
			contentType: "application/json",
			data: JSON.stringify(vm.pythonarticleinfo),
			success: function (r) {
				if (r.code === 0) {
					alert('操作成功', function(index){
						$("#jqGrid").trigger("reloadGrid");
					});
				} else {
					alert(r.msg);
				}
			}
		});
	});
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		pythonarticleinfo: {},
		bArticleInfo:{},
		q:{
			articleTitle:"",
			brandName:"",
			weixinNickname:""
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.pythonarticleinfo = {};
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			var url = vm.pythonarticleinfo.id == null ? "sys/pythonarticleinfo/save" : "sys/pythonarticleinfo/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.pythonarticleinfo),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "sys/pythonarticleinfo/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get(baseURL + "sys/pythonarticleinfo/info/"+id, function(r){
                vm.pythonarticleinfo = r.pythonarticleinfo;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var name=$("#brandNames").val();
			var url=baseURL + 'sys/pythonarticleinfo/queryList';
			if((name==""||name==null)&&vm.q.weixinNickname==""&&vm.q.articleTitle==""){
				window.location.reload();
			}
			var page = 1;
			console.log(name);
			console.log(url);
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page,
				url:url,
				postData:{"weixinNickname":vm.q.weixinNickname,"brandName":name,"articleTitle":vm.q.articleTitle}
            }).trigger("reloadGrid");
		},
		updateInfo: function (event) {
			var url =  "sys/pythonarticleinfo/updateInfo";

			//vm.bArticleInfo.articleBrandId=$('#articleBrandId option:selected').val();


			//console.log("bArticleInfo Save:"+vm.bArticleInfo);
			// if($('#brandName').val().trim()=="" && $('#articleTypeName').val().trim()==""){
			// 	alert("请选择品牌或者分类,注意只能选择其中一项");
			// 	return;
			// }
			//
			// if($('#brandName').val().trim()!="" && $('#articleTypeName').val().trim()!=""){
			// 	alert("品牌或者分类,只能选择其中一项");
			// 	return;
			// }
			//
			// if($('#brandName').val().trim()!="") {
			// 	vm.bArticleInfo.brandName = $('#brandName').val();
			// }else if($('#articleTypeName').val().trim()!="") {
			// 	vm.bArticleInfo.brandName = $('#articleTypeName').val();
			// }
			// console.log( "vm.bArticleInfo.brandName:"+vm.bArticleInfo.brandName);

			vm.bArticleInfo.articleContent =window.current_editor.getContent();

			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json;charset=UTF-8",
				data:JSON.stringify(vm.bArticleInfo),
				dataType: "JSON",
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							window.close();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		importHtmlAll: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}

			confirm('确定导入选中的 '+ids.length+'篇 文章？点击"确认"后离等待系统反馈后再继续操作其它功能！', function() {
				$.ajax({
					type: "POST",
					url: baseURL + "sys/barticleinfo/importHtmlAll",
					contentType: "application/json",
					data: JSON.stringify({"ids":ids}),
					datatype: "json",
					async:false,
					success: function (r) {
						if (r.code === 0) {
							alert('成功导入 '+r.total+'篇 文章', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						} else {
							alert(r.msg);
						}
					}
				});
			});
		},
        importHtml: function (event) {
            var ids = getSelectedRows();
            if(ids == null){
                return ;
            }

            var brandName=$("#brandNames").val();
            if(brandName == ""){
                alert("请选择要入库的品牌/分类.");
                return ;
            }

            console.log("brandName:"+brandName);

            confirm('确定导入选中的 '+ids.length+'篇 文章？', function() {
                $.ajax({
                    type: "POST",
                    url: baseURL + "sys/barticleinfo/importArticle1",
                    contentType: "application/json",
                    data: JSON.stringify({"ids":ids,"brandName":brandName}),
                    datatype: "json",
                    success: function (r) {
                        if (r.code === 0) {
                            alert('成功导入 '+r.total+'篇 文章', function(index){
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        }
	}
});